package client;

import java.awt.EventQueue;
import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;


import ClientServer.ClientSide;
import ClientServer.ProtocolIF;
import GUI.ClientSideGUI;
import GUI.ConsoleGUI;
import GUI.MainFrame;
import Log.Log;

public class ClientConsole  implements ProtocolIF ,Runnable{

	// varibles
	int DEFUALT_PORT  = ProtocolIF.DEFAULT_PORT;	
	ClientSide client;
	 String message;
	 BufferedReader fromConsole;
	 static ClientSideGUI GetDataFromGUI; 
	 static ClientConsole CC;
	public boolean bConnection;
	//constructors
	public ClientConsole(String host, int port) 
	  {
	    client= new ClientSide(host, port, this);
	    bConnection = client.getisConnected();
	    new Thread(this).start();
	  }

	
	@Override
	public void display(String message) 
	  {
	    MainFrame.frame.getConsoleRef().setMessageToScreen(message);
	  }
	
	public void accept() 
	  {
	   
	  }
	
	
	public void SendMsgToServer(Object msg){
		try {
			client.sendToServer(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.addError("Socket failed maybe server is down");
			MainFrame.frame.ServerDownMSG();
			e.printStackTrace();
		}
	}
	


	@Override
	public void run() {
		//client.EstablishConnection();
		 try
		    {
		       fromConsole = 
		        new BufferedReader(new InputStreamReader(System.in));
		    } 
		    catch (Exception ex) 
		    {
		     Log.addError
		        ("Unexpected error while reading from console!");
		    }
		while(true){
		// TODO Auto-generated method stub
		try {
			if (fromConsole.ready())
				message = fromConsole.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (message!=null)
			client.handleMessageFromClientUI(message);
		}//while
	}

	public static void main(String[] args) //Simplify the client
	  {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//ClientSideGUI frame = new ClientSideGUI();
					//frame.setVisible(true);
					GetDataFromGUI = new ClientSideGUI();
				    MainFrame MYMainFrame = new MainFrame(GetDataFromGUI);
				    MYMainFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		/*
	    String host = "";
	   
	    
	    int port = 0;  //The port number
	    GetDataFromGUI = new ClientSideGUI();
	    MainFrame MYMainFrame = new MainFrame(GetDataFromGUI);
	    MYMainFrame.setVisible(true);
	    while (GetDataFromGUI.getConnectionStatus());
	     CC= new ClientConsole(GetDataFromGUI.IP,GetDataFromGUI.PORT);
	    
	    MYMainFrame.getContentPane().remove(GetDataFromGUI);
	    CC.run();	
	    //GetDataFromGUI.run();
	    ConsoleGUI Console = new ConsoleGUI();
	   // MYMainFrame.add();
	    MYMainFrame.addCompponenetToMainFrame(Console);
	    */
	  }


	@Override
	public void printQuerey(ResultSet msg) {
		// TODO Auto-generated method stub
		display("id "+" | " +"name" +" |"+" price "+ "| owner ");
		try {
			while(msg.next())
			{
				 // Print out the values
				display(msg.getString(1)+" | " +msg.getString(2)+" |"+msg.getString(3)+" |"+msg.getString(4));
				//MainFrame.frame.SetScrolltoMax();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	

}
